package com.jason.www.widget.dialogfragment;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jason.www.R;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author：Jason
 * @date：2020/12/16 17:08
 * @email：1129847330@qq.com
 * @description:
 */
public class AlertDialogFragment extends BaseDialogFragment {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_ensure)
    TextView tvEnsure;
    private DialogData mDialogData;

    public AlertDialogFragment setDialogData(DialogData mDialogData) {
        this.mDialogData = mDialogData;
        return this;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        if (mDialogData != null) {
            if (!TextUtils.isEmpty(mDialogData.getTitle())) {
                tvTitle.setText(mDialogData.getTitle());
            }
            if (!TextUtils.isEmpty(mDialogData.getMessage())) {
                tvMessage.setText(mDialogData.getMessage());
            }
            if (!TextUtils.isEmpty(mDialogData.getPositiveButtonText())) {
                tvEnsure.setText(mDialogData.getPositiveButtonText());
            }
            if (!TextUtils.isEmpty(mDialogData.getNegativeButtonText())) {
                tvCancel.setText(mDialogData.getNegativeButtonText());
            }
            tvCancel.setVisibility(mDialogData.isIsNegativeButtonGone() ? View.GONE : View.VISIBLE);
            tvEnsure.setVisibility(mDialogData.isIsPositiveButtonGone() ? View.GONE : View.VISIBLE);
            setCancelable(mDialogData.isCancelable());
        }
    }

    @Override
    public int getAnimation() {
        return 0;
    }

    @Override
    public int getWidth() {
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }

    @Override
    public int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.dialog_alert;
    }

    @OnClick({R.id.tv_cancel, R.id.tv_ensure})
    public void onViewClicked(View view) {
        if (mDialogData == null) {
            dismiss();
            return;
        }
        switch (view.getId()) {
            case R.id.tv_cancel:
                if (mDialogData.getOnNegativeButtonClickListener() != null) {
                    mDialogData.getOnNegativeButtonClickListener().onClick(view);
                }
                break;
            case R.id.tv_ensure:
                if (mDialogData.getOnPositiveButtonClickListener() != null) {
                    mDialogData.getOnPositiveButtonClickListener().onClick(view);
                }
                break;
        }
        dismiss();
    }

    public interface OnPositiveButtonClickListener {
        void onClick(View view);
    }

    public interface OnNegativeButtonClickListener {
        void onClick(View view);
    }
}